<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background: #ffffff;
            }
            .aui-bar {
                margin-top: 0rem;
            }
            #demo .aui-bar-btn-item.aui-active {
                background-color: transparent;
                color: #212121;
            }
        </style>
    </head>
    <body>
        <section id="passwordInput" class="aui-content" style="display: none">
            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-form-list">
                    <li class="aui-list-header"></li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-lock"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="passwordText" type="password" placeholder="请输入新密码">
                            </div>
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-hide"></i>
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-laud"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="passwordText2" type="password" placeholder="请再次输入新密码">
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div style="padding: 1rem 1rem 1rem 1rem;">
                <div id="resetPassword" class="aui-btn aui-btn-info aui-btn-block" tapmode onclick="resetPassword()">
                    重置密码
                </div>
            </div>
        </section>
        <section id="verifyCodeInput" class="aui-content" style="display: block">
            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-form-list">
                    <li class="aui-list-header"></li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-mobile"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="phoneText" type="tel" placeholder="请输入手机号">
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon" style="width: auto">
                                <i class="aui-iconfont aui-icon-lock"></i>
                            </div>
                            <div class="aui-list-item-input" style="width: 55%">
                                <input id="verifyCodeText" type="text" placeholder="请输入验证码">
                            </div>
                            <div id="verifyCodeBtn" class="aui-btn aui-btn-info aui-btn-block aui-btn-sm" onclick="getVerifyCode()" style="margin: 0.1rem 0.3rem 0.1rem 0.3rem; width: 35%">
                                获取验证码
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div style="padding: 1rem 1rem 1rem 1rem;">
                <div id="regNextStep" class="aui-btn aui-btn-info aui-btn-block" tapmode onclick="regNextStep()">
                    下一步
                </div>
            </div>
        </section>
    </body>
    <script type="text/javascript" src="../../script/api.js" ></script>
    <script type="text/javascript" src="../../script/aui-tab.js" ></script>
    <script type="text/javascript" src="../../script/aui-toast.js" ></script>
    <script type="text/javascript" src="../../script/aui-dialog.js" ></script>
    <script type="text/javascript" src="../../script/jquery-3.2.0.min.js" ></script>
    <script type="text/javascript" src="../../script/md5.js" ></script>
    <script>
        var toast = new auiToast();
        var dialog = new auiDialog();
        function togglePannel() {
            var passwordInput = document.getElementById("passwordInput");
            var verifyCodeInput = document.getElementById("verifyCodeInput");
            passwordInput.style.display = "block";
            verifyCodeInput.style.display = "none";
        }

        function countDownVerifyCode() {
            var verifyCodeBtn = document.getElementById("verifyCodeBtn");
            verifyCodeBtn.textContent = "60秒后重新获取";
            verifyCodeBtn.removeAttribute('onclick');
            var id = setInterval(countDown, 1000);
            var count = 60;
            function countDown() {
                if (count == 0) {
                    clearInterval(id);
                    verifyCodeBtn.textContent = "获取验证码";
                    verifyCodeBtn.setAttribute('onclick', 'getVerifyCode()');
                } else {
                    count--;
                    verifyCodeBtn.textContent = count + "秒后重新获取";
                    verifyCodeBtn.removeAttribute('onclick');
                }
            }

        }

        apiready = function() {
            api.setRefreshHeaderInfo({
                visible : true,
                bgColor : '#ccc',
                textColor : '#fff',
                textDown : '下拉刷新...',
                textUp : '松开刷新...',
                showTime : true
            }, function(ret, err) {
                api.refreshHeaderLoadDone();
            });
            api.parseTapmode();
        }
        function getVerifyCode() {
            var phone = document.getElementById('phoneText').value;
            var pwPhoneText = parseInt(phone);
            if (isNaN(pwPhoneText) || pwPhoneText > 99999999999 || pwPhoneText < 10000000000) {
                toast.fail({
                    title : "号码错误！",
                    duration : 1000
                });
                return;
            }
            countDownVerifyCode();
            toast.loading({
                title : "请稍候",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var smsVerify = api.require('smsVerify');
            smsVerify.register(function(ret, err) {
                if (ret.status) {
                    //api.alert({msg: '注册成功'});
                } else {
                    api.alert({
                        msg : err.code + ' 注册失败'
                    });
                }
            });
            smsVerify.sms({
                phone : phone,
                country : "86"
            }, function(ret, err) {
                if (ret.status) {
                    if (ret.smart == true) {// 安卓版特有功能 智能验证
                        toast.hide();
                        toast.success({
                            title : "本机之前已通过验证！请直接输入新密码！",
                            duration : 2000
                        });
                        togglePannel();
                    } else {
                        toast.hide();
                        toast.success({
                            title : "短信发送成功！",
                            duration : 1000
                        });
                    }
                } else {
                    api.alert({
                        msg : err.code + ' ' + err.msg
                    });
                }
            });
        }

        function regNextStep() {
            //alert(ret.index);
            var phoneText = document.getElementById('phoneText').value;
            var verifyCodeText = document.getElementById('verifyCodeText').value;
            var pwPhoneText = parseInt(document.getElementById("phoneText").value);
            if (isNaN(pwPhoneText) || pwPhoneText > 100000000000 || pwPhoneText < 1) {
                toast.fail({
                    title : "号码错误！",
                    duration : 1000
                });
                return;
            }
            toast.loading({
                title : "验证中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var smsVerify = api.require('smsVerify');
            smsVerify.register(function(ret, err) {
                if (ret.status) {
                    //api.alert({msg: '注册成功'});
                } else {
                    api.alert({
                        msg : err.code + ' 注册失败'
                    });
                }
            });
            smsVerify.verify({
                phone : phoneText,
                country : "86",
                code : verifyCodeText
            }, function(ret, err) {
                if (ret.status) {
                    toast.hide();
                    toast.success({
                        title : "验证成功！",
                        duration : 1000
                    });
                    togglePannel();
                } else {
                    toast.hide();
                    toast.fail({
                        title : "验证失败！",
                        duration : 1000
                    });
                }
            });
            //togglePannel();
        }

        function closeWin() {
            api.sendEvent({
                name : 'ev_finishResetPW',
            });
        }

        function resetPassword() {
            var pwPhoneText = parseInt(document.getElementById("phoneText").value);
            var passwordText = document.getElementById("passwordText").value;
            var passwordText2 = document.getElementById("passwordText2").value;
            if (passwordText != passwordText2) {
                toast.fail({
                    title : "密码不匹配！",
                    duration : 1000
                });
                return;
            }
            if (passwordText.length < 8) {
                toast.fail({
                    title : "长度不足！至少需要8个字符！",
                    duration : 1000
                });
                return;
            }
            var model = api.require('model');
            model.config({
                appId : 'A6928874653494',
                appKey : '4B6C428C-24C2-F9E7-3175-F983922E8C8F',
                host : 'https://d.apicloud.com'
            });
            toast.loading({
                title : "重置密码中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var query = api.require('query');
            var queryId = 0;
            var userId = "";
            query.createQuery(function(ret, err) {
                if (ret) {
                    queryId = ret.qid;
                    if (queryId) {
                        query.whereEqual({
                            qid : queryId,
                            column : 'username',
                            value : pwPhoneText
                        });
                        model.findAll({
                            class : "user",
                            qid : queryId
                        }, function(ret, err) {
                            if (ret) {
                                //alert(JSON.stringify(ret));
                                userId = ret[0].id;
                                var model = api.require('model');
                                model.updateById({
                                    class : 'user',
                                    id : userId,
                                    value : {
                                        password : passwordText,
                                        passwordBackup : passwordText
                                    }
                                }, function(ret2, err) {
                                    //alert(JSON.stringify(ret2));
                                    if (ret2) {
                                        if (ret2.error) {
                                            toast.hide();
                                            var errMsg = "";
                                            if (ret2.error.statusCode == 202) {
                                                errMsg = "错误的手机号！";
                                            } else {
                                                errMsg = ret2.error.message;
                                            }
                                            dialog.alert({
                                                title : "重置失败！错误原因：",
                                                msg : errMsg,
                                                buttons : ['确定']
                                            });
                                        } else {
                                            resetForumPassword(ret2);
                                        }
                                    } else {
                                        toast.hide();
                                        toast.fail({
                                            title : "重置失败！",
                                            duration : 1000
                                        });
                                    }
                                });
                            } else {
                                alert(JSON.stringify(err));
                            }
                        });
                    }
                } else {
                    alert(JSON.stringify(err));
                }
            });
        }

        function resetForumPassword(userInfo) {
            //alert(JSON.stringify(userInfo));
            var tokenData = {
                identification : "admin",
                password : "Zxfy-2016",
            };
            $.ajax({
                "url" : "http://139.224.165.165/loanMarket/api/token",
                "type" : "POST",
                "cache" : false,
                "headers" : {
                    "Content-Type" : "application/json",
                },
                "data" : JSON.stringify(tokenData),
            }).done(function(data, status, header) {
                //success body
                //alert(JSON.stringify(data));
                //alert(JSON.stringify(status));
                //alert(JSON.stringify(header));
                var tokenHeader = 'Token ' + data.token;
                //alert('header: ' + tokenHeader);
                var attributes = {
                    username : "贷友" + userInfo.username.MD5(),
                    password : userInfo.passwordBackup,
                    email : userInfo.username
                };
                var data = {
                    attributes : attributes,
                };
                var body = {
                    data : data,
                };
                //alert(JSON.stringify(body));
                $.ajax({
                    "url" : "http://139.224.165.165/loanMarket/api/users/" + userInfo.forumId,
                    "type" : "PATCH",
                    "cache" : false,
                    "headers" : {
                        "Content-Type" : "application/json",
                        "Authorization" : tokenHeader
                    },
                    "data" : JSON.stringify(body),
                }).done(function(data, status, header) {
                    //success body
                    //alert(JSON.stringify(data));
                    //alert(JSON.stringify(status));
                    //alert(JSON.stringify(header));
                    toast.hide();
                    toast.success({
                        title : "重置成功！",
                        duration : 1000
                    });
                    var t = setTimeout("closeWin()", 500)
                }).fail(function(header, status, errorThrown) {
                    //fail body
                    //alert(JSON.stringify(header));
                    alert(JSON.stringify(status));
                    //alert(JSON.stringify(errorThrown));
                    toast.hide();
                    toast.success({
                        title : "重置成功！论坛重置失败！",
                        duration : 1000
                    });
                    var t = setTimeout("closeWin()", 500)
                })
            }).fail(function(header, status, errorThrown) {
                //fail body
                //alert(JSON.stringify(header));
                alert(JSON.stringify(status));
                //alert(JSON.stringify(errorThrown));
                toast.hide();
                toast.success({
                    title : "重置成功！论坛重置失败！",
                    duration : 1000
                });
                var t = setTimeout("closeWin()", 500)
            })
        }
    </script>
</html>